package io.gitee.zhucan123.core.infrastructure.exception.advice;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;


/**
 * @author: zhuCan
 * @date: 2019/8/24
 * @description:
 */
public abstract class AbstractExceptionAdvice<T extends RuntimeException> {

  protected ErrorDTO baseExceptionHandler(HttpServletRequest req, T ex) {
    String errorMessage = ex.getMessage();
    String errorURL = req.getRequestURL().toString();
    return new ErrorDTO(errorMessage, getStatus().value(), errorURL);
  }


  public abstract ErrorDTO exceptionHandler(HttpServletRequest req, T ex);

  protected HttpStatus getStatus() {
    try {
      ResponseStatus status = getClass().getMethod("exceptionHandler").getAnnotation(ResponseStatus.class);
      return status.value();
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    }
    return null;
  }
}
